#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName  :main_window.py
# @Time      :2023/12/6 22:51
# @Author    :retro_star
"""此文件用来写ui的主窗口界面
"""

from PyQt5.QtCore import Qt, QPoint
from PyQt5.QtWidgets import QWidget, QGroupBox, QPushButton, QBoxLayout, \
    QApplication, QDesktopWidget, QStatusBar, QStackedWidget
from functools import partial


class MainWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.mouse_is_pressing = False
        self.mouse_old_pos = None
        self.window_old_size = None
        self.central_window = CentralWindow()
        self.status_bar = QStatusBar()
        self.lay_out = QBoxLayout(QBoxLayout.TopToBottom, self)

    def ui_set(self):
        # 主窗口自身设置
        self.setMinimumSize(800, 800)
        self.move_to_screen_center()
        self.setWindowFlags(Qt.FramelessWindowHint)  # 隐藏系统自带的窗口边框
        self.setMouseTracking(True)
        # 状态条设置
        self.status_bar.setFixedHeight(30)
        self.status_bar.showMessage('Release 0.0.1')
        self.status_bar.setMouseTracking(True)
        # 向布局中添加组件
        self.lay_out.setContentsMargins(0, 0, 0, 0)
        WindowFlags(self, self.lay_out)
        self.lay_out.addWidget(self.central_window)
        self.lay_out.addWidget(self.status_bar)

    def move_to_screen_center(self):
        """移动窗口到屏幕的中心位置"""
        center_x = QDesktopWidget().screenGeometry().center().x()
        center_y = QDesktopWidget().screenGeometry().center().y()
        new_x = center_x - self.size().width() // 2
        new_y = center_y - self.size().height() // 2
        self.move(new_x, new_y)

    def mousePressEvent(self, a0):
        if self.cursor().shape() == Qt.CursorShape.SizeFDiagCursor:
            self.mouse_is_pressing = True
        self.mouse_old_pos = a0.globalPos()
        self.window_old_size = self.size()

    def mouseReleaseEvent(self, a0):
        self.mouse_is_pressing = False

    def mouseMoveEvent(self, a0):
        cur_mouse_x = a0.globalPos().x()
        cur_mouse_y = a0.globalPos().y()
        h_range = self.geometry().right() > cur_mouse_x > self.geometry().right() - 8
        v_range = self.geometry().bottom() > cur_mouse_y > self.geometry().bottom() - 8
        if h_range and v_range:
            # 光标在右下角就变换为拉伸形状
            self.setCursor(Qt.CursorShape.SizeFDiagCursor)
        else:
            # 其他情况光标就变会普通的箭头形状
            self.setCursor(Qt.CursorShape.ArrowCursor)
        if self.mouse_is_pressing:
            new_pos = QPoint(cur_mouse_x, cur_mouse_y)
            delta = new_pos - self.mouse_old_pos
            self.resize(self.window_old_size.width() + delta.x(),
                        self.window_old_size.height() + delta.y())


class WindowFlags(QGroupBox):
    """
    创建自己的窗口边框
    """

    def __init__(self, parent_window: QWidget, parent_layout: QBoxLayout):
        super().__init__()
        self.is_moving = False
        self.main_window_pos = None
        self.star_pos = None
        self.main_window = parent_window
        self.close_btn = QPushButton('close')
        self.max_btn = QPushButton('max')
        self.min_btn = QPushButton('min')
        self.lay_out = QBoxLayout(QBoxLayout.RightToLeft, self)
        self.ui_set()
        parent_layout.addWidget(self)

    def ui_set(self):
        self.setFixedHeight(40)
        self.close_btn.setFixedSize(60, 40)
        self.max_btn.setFixedSize(60, 40)
        self.min_btn.setFixedSize(60, 40)

        self.lay_out.setContentsMargins(0, 0, 0, 0)
        self.lay_out.setSpacing(0)
        self.lay_out.addWidget(self.close_btn)
        self.lay_out.addWidget(self.max_btn)
        self.lay_out.addWidget(self.min_btn)
        self.lay_out.addStretch()

        self.close_btn.clicked.connect(QApplication.instance().quit)
        self.min_btn.clicked.connect(self.main_window.showMinimized)
        self.max_btn.clicked.connect(self.max_btn_func)

    def max_btn_func(self):
        """放大按钮实现窗口的最大化和还原"""
        if self.main_window.isMaximized():
            self.main_window.showNormal()
        else:
            self.main_window.showMaximized()

    def mousePressEvent(self, event):
        """当鼠标在此控件范围按下，记录当前鼠标位置和主窗口位置，并将待移动状态置为真"""
        self.is_moving = True
        self.main_window_pos = self.main_window.pos()
        self.star_pos = event.globalPos()

    def mouseMoveEvent(self, event):
        """实现窗口移动功能"""
        if self.is_moving:
            delta = event.globalPos() - self.star_pos
            self.main_window.move(self.main_window_pos + delta)

    def mouseReleaseEvent(self, event):
        """当鼠标松开，将待移动状态置为假"""
        self.is_moving = False


class CentralWindow(QWidget):
    """中心窗口"""

    def __init__(self):
        super().__init__()
        self.lay_out = QBoxLayout(QBoxLayout.LeftToRight)
        self.choose_bar = QWidget()
        self.show_window = QStackedWidget()
        self.choose_bar_layout = QBoxLayout(QBoxLayout.TopToBottom)
        self.ui_set()

    def ui_set(self):
        # 中心窗口设置
        self.setLayout(self.lay_out)
        self.setContentsMargins(0, 0, 0, 0)
        # 选项栏设置
        self.choose_bar.setFixedWidth(200)
        self.choose_bar.setLayout(self.choose_bar_layout)
        # 选项栏布局设置
        self.choose_bar_layout.setContentsMargins(0, 0, 0, 0)
        self.choose_bar_layout.addStretch()
        # 添加控件到中心窗口的布局中
        self.lay_out.addWidget(self.choose_bar)
        self.lay_out.addWidget(self.show_window)

    def add_choose_btn(self, btn):
        """添加筛选栏的按钮"""
        index = self.choose_bar_layout.count() - 1
        self.choose_bar_layout.insertWidget(index, btn)

    def add_show_window(self, window, window_name):
        """向堆栈里压入窗口"""
        btn = QPushButton(window_name)
        self.add_choose_btn(btn)
        self.show_window.addWidget(window)
        btn.clicked.connect(partial(self.show_window.setCurrentWidget, window))
